package com.arch.dev.client.view;

import com.arch.dev.client.security.AuthService;
import com.arch.dev.client.security.AuthServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.StackLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class LeftHandNav extends Composite {

	private static LeftHandNavUiBinder uiBinder = GWT
			.create(LeftHandNavUiBinder.class);

	interface LeftHandNavUiBinder extends UiBinder<Widget, LeftHandNav> {
	}

	public LeftHandNav() {
		init();
		initWidget(uiBinder.createAndBindUi(this));
	}

	@UiField(provided = true)
	StackLayoutPanel leftHandNav;

	Anchor add;
	Anchor view1;
	Anchor view2;
	Anchor view3;
	Anchor edit;
	Anchor catagory;
	Anchor user;
	Anchor flexGraphView;

	private final AuthServiceAsync authService = GWT
			.create(AuthService.class);
	
	private void init() {
		// Create a new stack panel

		leftHandNav = new StackLayoutPanel(Unit.EM);

		leftHandNav.setPixelSize(220, 500);
		//leftHandNav.setSize("100%", "100%");

		
		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(4);

		
		add = new Anchor("Add");
		edit = new Anchor("Edit");
		

		vp.add(add);
		vp.add(edit);

		VerticalPanel vp1 = new VerticalPanel();
		vp1.setSpacing(4);

		view1 = new Anchor("Monthly Expenditure");

		vp1.add(view1);

		VerticalPanel vp2 = new VerticalPanel();
		vp2.setSpacing(4);

		view2 = new Anchor("Yearly Expenditure");

		vp1.add(view2);
		
		flexGraphView = new Anchor("Flex Graphs");

		vp1.add(flexGraphView);

		VerticalPanel vp3 = new VerticalPanel();

		view3 = new Anchor("Generate");

		vp2.add(view3);
		
		catagory = new Anchor("Catagory");
		
		final VerticalPanel catagoryVP = new VerticalPanel();

		catagoryVP.add(catagory);
		
		user = new Anchor("User");
		
		catagoryVP.add(user);
		
		leftHandNav.add(new SimplePanel(vp), "Budget", 2);

		leftHandNav.add(new SimplePanel(vp1), "Graph", 2);
		leftHandNav.add(new SimplePanel(vp2), "PDF", 2);
		
		authService.retrieveUserAuthority(new AsyncCallback<String>() {
			
			@Override
			public void onSuccess(String auth) {
				if(auth.equals("ROLE_ADMIN")){
					leftHandNav.add(new SimplePanel(catagoryVP), "Admin", 2);
				}			
			}
			
			@Override
			public void onFailure(Throwable arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	
	

	}

	public void addViewListener(ClickHandler handler) {
		add.addClickHandler(handler);
	}

	public void editViewListener(ClickHandler handler) {
		edit.addClickHandler(handler);
	}
	
	public void addView1Listener(ClickHandler handler) {
		view1.addClickHandler(handler);
	}

	public void addView2Listener(ClickHandler handler) {
		view2.addClickHandler(handler);
	}

	public void addView3Listener(ClickHandler handler) {
		view3.addClickHandler(handler);
	}

	public void addCatagoryViewListener(ClickHandler handler) {
		catagory.addClickHandler(handler);
	}
	
	public void addUserViewListener(ClickHandler handler) {
		user.addClickHandler(handler);
	}
	
	public void addflexGraphViewListener(ClickHandler handler){
		flexGraphView.addClickHandler(handler);
	}
}
